#!/usr/bin/env python3

"""
实现装饰器
"""

import functools

def celebrator_1(func):
    """
    实现对被修饰的函数传递参数
    """
    def inner(*args, **kwargs):
        print("新增函数功能...")
        func(*args, **kwargs)
    return inner

def celebrator_2(char):
    """
    实现修饰函数进行传参数
    """
    def print_style(func):
        def inner():
            print(char * 15)
            func()
        return inner
    return print_style

def celebrator_3(parser, *keys):
    """
    返回参数:
        必要参数存在性验证装饰器
    """
    def deco(func):

        def base(*args, **kwargs):
            #  请求参数字典
            req_args = parser

            #  循环验证参数存在性
            if any([req_args.get(x) in [None, ''] for x in keys]):
                return {'code': 2}

            return func(*args, **kwargs)

        return base

    return deco

def log(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("call %s(): " % func.__name__)
        return func(*args, **kwargs)
    return wrapper


@celebrator_1
def run_1(a):
    print(a)

@celebrator_2('=')
@celebrator_2('*')
def run_2():
    print("python 联盟")


#  parser = {'account': "molio@123.com", 'passwd': "123456"}
parser = {'account': "molio@123.com", 'passwds': "123456"}
@celebrator_3(parser, 'account', 'passwd')
def run_3(parser=parser):
    print(parser)

@log
def speaking(content):
    print(content)

if __name__ == "__main__":
    #  run_1("molio")
    #  run_2()
    #  run_3()
    speaking("hello")
